package com.demo.javaDemo.sort;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.Test;


public class MapReduceDemo {

	@Test
	public void find() {
		  List<String> strs = Arrays.asList("d", "b", "a", "c", "a");
	        Optional<String> min = strs.stream().min(Comparator.comparing(Function.identity()));
	        Optional<String> max = strs.stream().max((o1, o2) -> o1.compareTo(o2));
	        System.out.println(String.format("min:%s; max:%s", min.get(), max.get()));// min:a; max:d
	 
	        Optional<String> aa = strs.stream().filter(str -> !str.equals("a")).findFirst();
	        Optional<String> bb = strs.stream().filter(str -> !str.equals("a")).findAny();
	 
	        Optional<String> aa1 = strs.parallelStream().filter(str -> !str.equals("a")).findFirst();
	        Optional<String> bb1 = strs.parallelStream().filter(str -> !str.equals("a")).findAny();
	 
	        System.out.println(aa.get() + "===" + bb.get());// d===d
	        System.out.println(aa1.get() + "===" + bb1.get());// d===b or d===c
	}
	@Test
	public void map() {
		List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
		// 获取对应的平方数
		List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
		System.out.println(squaresList);
	}
	@Test
	public void collect() {
		List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
		List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
		 
		System.out.println("筛选列表: " + filtered);
		String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
		System.out.println("合并字符串: " + mergedString);
	}
}
